package com.zhengsr.wanandroid.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.zhengsr.corelib.bean.KnowTreeBean;
import com.zhengsr.viewpagerlib.indicator.TabIndicator;
import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.fragment.KnowledgeFragment;
import com.zhengsr.wanandroid.mvp.base.BaseActivity;
import com.zhengsr.wanandroid.mvp.base.BaseFragment;
import com.zhengsr.wanandroid.mvp.base.BasePresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class KnowledgeDetaileActivity extends BaseActivity {



    @BindView(R.id.common_toolbar)
    Toolbar mToolbar;

    @BindView(R.id.know_indicator)
    TabIndicator mIndicator;

    @BindView(R.id.know_viewpager)
    ViewPager mViewPager;


    private List<BaseFragment> mFragments ;

    @Override
    public int getLayoutId() {
        return R.layout.activity_knowledge_detaile;
    }

    @Override
    public BasePresent getPresent() {
        return null;
    }

    @Override
    public void initView() {
        KnowTreeBean bean = (KnowTreeBean) getIntent().getSerializableExtra("knowledge");
        if (bean == null){
            return;
        }
        initToolbar(bean.getName());
        initDetailView(bean);
        initViewpager(bean);


    }

    private void initViewpager(KnowTreeBean bean) {
        if (mFragments == null){
            mFragments = new ArrayList<>();
        }
        mFragments.clear();
        for (KnowTreeBean knowTreeBean : bean.getChildren()) {
            mFragments.add(KnowledgeFragment.newInstance(knowTreeBean.getId()));
        }

        mViewPager.setAdapter(new KnowPagAdapter(getSupportFragmentManager()));
    }

    private void initDetailView(KnowTreeBean bean) {
        // 设置 viewpager的切换速度，这样点击的时候比较自然
        mIndicator.setViewPagerSwitchSpeed(mViewPager,600);
        List<String> mTitle = new ArrayList<>();
        for (KnowTreeBean treeBean : bean.getChildren()) {
            mTitle.add(treeBean.getName());
        }
        mIndicator.setTabData(mViewPager,mTitle, new TabIndicator.TabClickListener() {
            @Override
            public void onClick(int position) {
                //顶部点击的方法公布出来
                mViewPager.setCurrentItem(position);
            }
        });
    }

    private void initToolbar(String title){
        mToolbar.setTitle(title);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    class KnowPagAdapter extends FragmentPagerAdapter{

        public KnowPagAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments == null ? null : mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments == null ? 0 : mFragments.size();
        }
    }
}
